//
//  LXDiskManager.m
//  绿稀全球购
//
//  Created by 王敏 on 16/7/9.
//  Copyright © 2016年 SJT. All rights reserved.
//
#import "LXDiskManager.h"


@implementation LXDiskManager
+(instancetype)shareManager
{
    static dispatch_once_t onceToken;
    static LXDiskManager * manager;
    dispatch_once(&onceToken, ^{
        manager = [[LXDiskManager alloc] init];
    });
    return manager;
}

+(float)getCacheSize
{
//    NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString * cleanPath = paths.firstObject;
//    NSString * filterPath = [cleanPath stringByAppendingPathComponent:@"LvXiShopping"];
    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    long long folderSize = 0;
    if ([fileManager fileExistsAtPath:cleanPath]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:cleanPath];
        for (NSString *fileName in childerFiles) {
            NSString *absolutePath=[cleanPath stringByAppendingPathComponent:fileName];
            folderSize +=[LXDiskManager fileSizeAtPath:absolutePath];
        }
    }
//    NSLog(@"%@",paths);
    SDImageCache * sdcacheManager = [SDImageCache sharedImageCache];
    folderSize += (float)[sdcacheManager getSize];
    return folderSize/1024.0/1024.0;
}

+(CGFloat)fileSizeAtPath:(NSString *)path
{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:path])
    {
        if(![path containsString:@"LvXiShopping"])
        {
    
            CGFloat size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
        
            return size;
        }
    }
    return 0;
}

+(void)cleanCache:(void (^)())complete
{
   
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString * cleanPath = paths.firstObject;
        NSFileManager *fileManager=[NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:cleanPath]) {
            NSArray *childerFiles=[fileManager subpathsAtPath:cleanPath];
            for (NSString *fileName in childerFiles) {
                if([cleanPath containsString:@"LvXiShopping"]) break;
                NSString *absolutePath=[cleanPath stringByAppendingPathComponent:fileName];
                [fileManager removeItemAtPath:absolutePath error:nil];
            }
        }
        [[SDImageCache sharedImageCache] cleanDisk];
            dispatch_sync(dispatch_get_main_queue(), ^{
                if(complete)
                {
                    complete();
                }
            });
    });


}



@end
